package org.redmagic.utils;

import java.io.Closeable;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class Closeables {

    private static final Log LOG = LogFactory.getLog(Closeables.class);
    
    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                close(closeable, true);
            } catch (IOException ioe) {
                LOG.error("IOException should not have been thrown.");
            }
        }
    }
    
    public static void close(Closeable closeable, boolean swallowException) throws IOException {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ioe) {
                if (swallowException) {
                    LOG.error(ioe.getMessage(), ioe);  
                } else {
                    throw ioe;
                }
            }
        }
    }
}
